<?php 
   require_once 'config.php';
   $adt_id = getValue('id');
   $author = new author($adt_id);
   $full_mod = author::getListPermisstion();
   $author_mod = $author->list_permisstion;
   $author_mod_id = array();
   if(count($author_mod) > 0){
      foreach($author_mod as $value){
         $author_mod_id[] = $value['mod_id'];
      }
         
   }
   $action = getValue('action','str','POST');
   if($action == 'submit'){
      $username = getValue('username','str','POST');
      $pass = getValue('pass','str','POST','');
      $repass = getValue('repass','str','POST','');
      if($pass != ''){
         if($pass === $repass){
            $db_update = new db_execute("UPDATE administrator SET adt_password ='" . md5($pass) ."' WHERE adt_id = " . $adt_id);
            $alert = Str::alert("Cập nhật thành công");
         }else{
            $alert = Str::alert("Xác nhận mật khẩu không chính xác","alert-error");
         }
      }
      $array_mod_id = getValue('mod_id','arr','POST',array());
      if(count($array_mod_id) > 0){
         $value_insert = "VALUES";
         foreach($array_mod_id as $value){
            $value_insert .= "(" . $adt_id . "," . $value . "),";
         }   
         $value_insert = substr($value_insert,0,-1);
         $db_del = new db_execute("DELETE FROM author WHERE adt_id =" . $adt_id);
         $db_insert = new db_execute("INSERT IGNORE author(adt_id,mod_id)" . $value_insert);
         unset($db_del);
         $full_mod = author::getListPermisstion();
      }
   }
   
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Thêm mới <?=$moduol_name?></title>
   <?=$css_module?>
   <?=$js_module?>
</head>
<div class="row-fluid">
   <div class="head">
      <div class="isw-grid"></div>
      <h1>Thêm mới <?=$moduol_name?></h1>      
      <div class="reload_tab" title="Reload"></div>                         
      <div class="clear"></div>
   </div>
   <div class="block-fluid">
      <?php 
         echo isset($alert) ?  $alert : '';
      ?>
      <form class="form-horizontal" name="add" action="" method="POST" enctype="multipart/form-data"  onsubmit="">
         <input type="hidden" name="action" value="submit" />
         <fieldset>
            <div class="control-group">
               <label class="control-label" for="input01">Tên đăng nhập</label>
               <div class="controls">
						<input type="text" class="input-xlarge tooltip-right span3" value="<?=$author->name?>" id="username" name="username" title="Tên đăng nhập"/>
					</div>
            </div>      
            <div class="control-group">
               <label class="control-label" for="input01">Mật khẩu mới</label>
               <div class="controls">
						<input type="password" class="input-xlarge tooltip-right span3" id="pass" name="pass" title="Mật khẩu mới"/>
					</div>
            </div> 
            <div class="control-group">
               <label class="control-label" for="input01">Xác nhận mật khẩu</label>
               <div class="controls">
						<input type="password" class="input-xlarge tooltip-right span3" id="repass" name="repass" title="Gõ lại mật khẩu"/>
					</div>
            </div>       
            <div class="control-group">
               <label class="control-label" for="input01">Quyền</label>
               <div class="controls">
						<ul class="list-permission">
                     <?
                        foreach($full_mod as $value){
                           if(in_array($value['mod_id'],$author_mod_id)){
                              $classCheck = 'checked';
                              $checked = 'checked="checked" ';
                           }else{
                              $classCheck = '';
                              $checked = '';
                           }
                     ?>
                           
                           <li>
                              <div class="checker">
                                 <span class="<?=$classCheck?>">
                                    <input type="checkbox" class="checkbox-row" name="mod_id[]" value="<?=$value['mod_id']?>" <?=$checked?>"/>
                                 </span>
                              </div>
                              <span><?=$value['mod_name']?></span>
                           </li>
                        <?}
                     ?>
                  </ul>
                  
					</div>
            </div>
            <div class="form-actions">
					<button type="submit" class="btn btn-info">Lưu lại</button>
					<button type="reset"  class="btn btn-warning">Hủy</button>
				</div>
         </fieldset>
      </form>
   </div> 
</div>